/**
 * Jaxson ajax
 *
 * Copyright Remedy Interactive, licensed under Apache version 2 license:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0.html
 *
 * This notice must remain in all java source files.
 */
package org.jaxson.examples.charts;

import java.util.ArrayList;

import org.jaxson.examples.charts.customchart.WaterfallChart;
import org.jaxson.struts.BaseAction;
import org.jaxson.struts.charts.bar.BarChart;
import org.jaxson.struts.charts.pie.PieChart;
import org.jaxson.struts.encoder.ResponseContentType;

/**
 * @author Joe Maisel
 *
 */
public class ImageTestAction extends BaseAction
{
	private enum ChartType {pie, bar, waterfall};
	private ChartType type = ChartType.pie;
	
	/**
	 * @see org.jaxson.struts.BaseAction#handle()
	 */
	@Override
	public String handle()
	{
		// enable and disable response encodings
		enableResponseType( ResponseContentType.jpg );
		enableResponseType( ResponseContentType.xml );
		enableResponseType( ResponseContentType.json );
		disableResponseType( ResponseContentType.html );

		// Get a model.....
		ArrayList<ChartTestModel> model = new ArrayList<ChartTestModel>();
		setModel( model );


		for( int i=0; i<100; i++ )
		{
			ChartTestModel m = new ChartTestModel();
			m.setNum( i );
			m.setOtherNum( i*10 );
			model.add( m );
		}
		
		// determine which type of chart to render
		// based on what the client has requested....
		if( this.getResponseContentType() == ResponseContentType.jpg )
		{
			switch( this.type )
			{
				case pie:
					setChart( new PieChart() );
					break;
					
				case bar:
					setChart( new BarChart() );
					break;
					
				case waterfall:
					setChart( new WaterfallChart() );
					break;
			}
		}
		
		return null;
	}
	
	public void setChartType( String type )
	{
		this.type = ChartType.valueOf( type );
	}

	private static final long serialVersionUID = -2017497052868210711L;
}
